from datetime import datetime
from typing import Optional, List, Union, Dict, Any

from pydantic import Field, BaseModel, validator

from models.administrative_division import AdministrativeDivision
from models.alumni import Alumni
from models.business import Business
from models.common import RWModel, OID
from models.fileserver import FileEntity
from models.institution import Institution


class Activity(RWModel, AdministrativeDivision):
    """
    活动信息
    """
    id: Optional[OID] = Field(title="活动ID")
    period_or_year: Optional[str] = Field(title="活动举办期数/年份")
    name: Optional[str] = Field(title="活动名称")
    address: Optional[str] = Field(title="活动地点")
    start_time: Optional[datetime] = Field(title="活动开始时间")
    end_time: Optional[datetime] = Field(title="活动结束时间")
    duration: Optional[float] = Field(title="活动时长")
    people_num: Optional[int] = Field(title="参与人数")
    topics: Optional[List[str]] = Field(title="活动主题")
    abstract: Optional[str] = Field(title="简介")

    # 引用类型字段ref
    business_id: Optional[OID] = Field(default=None, title="所属业务ID")
    business_name: Optional[str] = Field(max_length=120, default=None, title='业务')
    institution_id: Union[OID, None] = Field(title="组织部门ID")
    # deprecated
    participate_insts: Optional[List[OID]] = Field(default=[], title="参与机构/部门ID")
    co_inst_ids: Optional[List[OID]] = Field(default=[], title="协办单位/允许")

    images: Optional[List[FileEntity]] = Field(title="互动图片")
    videos: Optional[List[FileEntity]] = Field(title="互动视频")
    appendixes: Optional[List[FileEntity]] = Field(title="活动附件")
    #  科协性质
    in_host_kexie: Optional[int] = 0
    in_co_kexie: Optional[int] = 0

    # 只显示不存储
    institution: Optional[Institution]
    business: Optional[Business]
    participates: Optional[List[Institution]]
    co_institutions: Optional[List[Institution]]

    co_inst_label: Optional[str]
    institution_label: Optional[str]
    # new add
    activity_type: Optional[str] = Field(title="活动类型")
    importance_of_activities: Optional[str] = Field(None, title="活动的重要性")  # importance_of_activities
    dept: Optional[Dict[Any, Any]] = Field(title="部门")
    tags: Optional[List[str]] = Field(title='标签管理')
    activity_type_two: Optional[str]

    # cache
    host_label: Any = None
    cost_labels: Any = None
    children: int = 0

    @validator("co_inst_ids", pre=True, always=True)
    def validate_co_inst_ids_with_default(cls, v):
        if not v:
            return []
        return v

    def fill_file_url(self):
        entities: List[FileEntity] = []
        if self.images:
            entities.extend(self.images)
        if self.videos:
            entities.extend(self.videos)
        if self.appendixes:
            entities.extend(self.appendixes)
        for e in entities:
            e.fill_url()

    def unstore_file_url(self):
        entities: List[FileEntity] = []
        if self.images:
            entities.extend(self.images)
        if self.videos:
            entities.extend(self.videos)
        if self.appendixes:
            entities.extend(self.appendixes)
        for e in entities:
            e.unstore_url()


reasons = ["时间不合适", "没有打通电话", "身体原因", "没有兴趣", "单纯不想参加", "相关性不大", "其他"]


class ActivityRegister(RWModel):
    """
    活动注册信息
    """
    id: Optional[OID]
    person_id: Optional[OID] = Field(title="参与校友ID")
    activity_id: Optional[OID] = Field(title="活动ID")
    role: Optional[str] = Field(title="活动担任角色")
    level_of_role: Optional[str] = Field(title="角色重要性")
    contribution: Optional[float] = Field(title="角色贡献度")
    alumni_score: Optional[float] = Field(title="校友值")
    is_attended: Optional[bool] = Field(title="是否出席")
    reasons_non_attended: Optional[List[str]] = Field(title="未出席原因")
    internal_contacts: Optional[List[str]] = Field(title="内部联系人")
    relationship_labels_activity_persons: Optional[List[str]]
    per_act_time: Optional[float] = Field(title="参与时长")
    refs_participants: Optional[int] = Field(default=0, title="参与时长")

    # 只显示不存储
    activity: Optional[Activity]
    person: Optional[Alumni]

    @property
    def combined_id(self):
        return str(self.person_id), str(self.activity_id)


class ActivityInstitution(RWModel):
    """
    活动机构注册信息
    """
    id: Optional[OID]
    institution_id: Optional[OID] = Field(title="参与机构ID")
    activity_id: Optional[OID] = Field(title="活动ID")
    is_attended: Optional[bool] = Field(title="是否出席")

    # 只显示不存储
    activity: Optional[Activity]
    institution: Optional[Institution]

    @property
    def combined_id(self):
        return str(self.institution_id), str(self.activity_id)

class ValidateModel(BaseModel):
    label: Optional[str]
    institution_id: Optional[str]


class PermModel(BaseModel):
    perms: Optional[List[str]] = [1, 2]


# 活动-人才关联表
class RoleAlumni(BaseModel):
    activity_id: Optional[str]
    person_id: Optional[str]
    role: Optional[str]
    level_of_role: Optional[str]
    is_attended: Optional[bool] = Field(default=True, title="是否出席")
    images: Optional[List[Dict[str, Any]]]
    videos: Optional[List[Dict[str, Any]]]
    appendixes: Optional[List[Dict[str, Any]]]
    relationship_labels_activity_persons: Optional[List[str]]
    per_act_time: Optional[float] = Field(title="参与市场")  # 新增时长


if __name__ == '__main__':
    print(Activity.schema())
